<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="150px" label-position="left">
			<el-form-item label="考核计划标题" prop="title">
				<el-input v-model="form.title" placeholder="考核计划标题"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="排序号" prop="sortNo">
						<el-input-number v-model="form.sortNo" placeholder="排序号"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="考核规则" prop="kpiRule">
						<sc-select v-model="form.kpiRule" :params="{code:'kpiRuleType'}" :apiObj="$API.hr.hrDic.getHrDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="考核时间段(开始)" prop="beginTime">
						<el-date-picker v-model="form.beginTime" placeholder="考核时间段(开始)" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			<el-col :lg="12">
				<el-form-item label="考核时间段(结束)" prop="endTime">
					<el-date-picker v-model="form.endTime" placeholder="考核时间段(开始)" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
				</el-form-item>
			</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="考核负责人" prop="chargeUser">
						<select-user-dialog v-model:accountId="form.chargeUser" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="被考核人" prop="userRole">
						<select-user-dialog v-model:accountId="form.userRole" :mode="true"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="被考核部门" prop="deptRole">
						<select-dept-dialog v-model:deptId="form.deptRole" :mode="true"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="被考核职务" prop="levelRole">
						<select-user-level-dialog v-model:levelId="form.levelRole" :mode="true"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="考核指标集" prop="itemId">
						<sc-select v-model="form.itemId"  :apiObj="$API.hr.hrKpiItem.getHrKpiItemListForSelect" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" placeholder="备注说明" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'hr'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="hr-kpi"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from '@/views/module/attachMenu'
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
		selectDeptDialog,
		selectUserLevelDialog,
		msgBoxCheckBox,
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			btName: "保存",
			titleMap: {
				add: '添加KPI考核计划',
				edit: '编辑KPI考核计划',
				show: '查看'
			},
			fileList: [],
			apiObj: this.$API.file.attach.uploadFile,
			templateList: [],
			templates: [],
			form: {
				planId: '',
				sortNo:0,
				title: '',
				kpiRule: '',
				beginTime: '',
				endTime:'',
				itemId:'',
				chargeUser:'',
				userRole: '',
				deptRole: '',
				levelRole: "",
				institutionName: "",
				attachId: '',
				remark: ''
			},
			rules: {
				title:[
					{required: true, message: '考核计划标题不能为空'}
				],
				itemId: [
					{required: true, message: '考核指标不能为空'}
				],
				beginTime: [
					{required: true, message: '考核时间段(开始)不能为空'}
				],
				endTime: [
					{required: true, message: '考核时间段(结束)不能为空'}
				],
				kpiRule:[
					{required: true, message: '考核规则不能为空'}
				],
				chargeUser:[
					{required: true, message: '考核负责人不能为空'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.hr.hrKpiPlan.insertHrKpiPlan.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.hr.hrKpiPlan.updateHrKpiPlan.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.planId = data.planId
			this.form.title = data.title
			this.form.sortNo = data.sortNo
			this.form.kpiRule = data.kpiRule
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.itemId = data.itemId
			this.form.chargeUser = data.chargeUser
			this.form.userRole = data.userRole
			this.form.deptRole = data.deptRole
			this.form.levelRole = data.levelRole
			this.form.institutionName = data.institutionName
			this.form.attachId = data.attachId
			this.form.remark = data.remark
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
